package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.bean.Inventory;
import com.example.demo.bean.InventoryDie;
import com.example.demo.service.SpreadSheet;
import com.example.demo.util.GetData;
import com.example.demo.util.LoginAndToken;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.net.URIBuilder;
import org.json.JSONArray;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;


@Service
public class SpreadSheetImpl implements SpreadSheet {
    @Override
    public List CommodityChickenDead(String date, String name,String isAudit,String isSend) throws URISyntaxException, IOException, ParseException {
        HashMap<Object, Object> inventoryNumberMap = new HashMap<>();
        LoginAndToken loginAndToken = new LoginAndToken();
        Map luoHeOrgIdMap = loginAndToken.getOrgIdByName(name);
        //农场对应的ID
        Iterator<Map.Entry<Object, Object>> orgIterator = luoHeOrgIdMap.entrySet().iterator();
        String url = "";
        ArrayList inventoryList = new ArrayList<>();
        url = "http://ny.shuanghui.net:9181/breed/bi/product/accident/list";
        URIBuilder uriBuilder;

        //设置参数
        while (orgIterator.hasNext()){
            uriBuilder = new URIBuilder(url);
            Map.Entry<Object, Object> next1 = orgIterator.next();
            uriBuilder.setParameter("startDate",date);
            uriBuilder.setParameter("endDate", date);
            uriBuilder.setParameter("orgId", next1.getValue().toString());
            //是否审核
            uriBuilder.setParameter("isAudit", isAudit);
            //是否上传
            uriBuilder.setParameter("isSend", isSend);
            uriBuilder.setParameter("pageNum", "1");
            uriBuilder.setParameter("pageSize", "30");
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            httpGet.setHeader("Token", LoginAndToken.token);
            CloseableHttpResponse response = LoginAndToken.httpClient.execute(httpGet);
            JSONObject data = GetData.getData(response);
            InventoryDie inventoryDie = new InventoryDie();//创建商品鸡事故只数信息对象
            String total = data.getString("total");
            if (total==null ){
                continue;
            }
            JSONObject totalJson = JSON.parseObject(total);
            Integer accidentNum = (Integer) totalJson.get("accidentNum");//事故只数
            inventoryDie.setAccidentNum(accidentNum);
            Integer subNum = (Integer) totalJson.get("invQty");//合计只数
            inventoryDie.setSubNum(subNum);
            Integer weedNum = (Integer) totalJson.get("weedNum");//淘汰只数
            inventoryDie.setWeedNum(weedNum);
            String page = data.getString("page");
            JSONObject pagesObject = JSON.parseObject(page);
            String list = pagesObject.getString("list");
            JSONArray jsonArray = new JSONArray(list);
            //将农场各个栋舍的存栏放到集合中
            HashMap<String, Integer> invQtyMap = new HashMap<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                org.json.JSONObject list1 = jsonArray.getJSONObject(i);
                //栋舍名称
                String houseName = list1.getString("houseName");
                //栋舍对应的死淘总数
                Integer invQty = (Integer) list1.get("invQty");
                invQtyMap.put(houseName,invQty);
//                inventory.setInvQty(invQty);
            }
            inventoryDie.setInvQty(invQtyMap);
//            System.out.println("存栏" + total);

            inventoryDie.setOrgName(next1.getKey().toString());

//        }
            inventoryList.add(inventoryDie);
        }
        return inventoryList;
    }
}
